<?php

namespace app\common\model;

use app\lib\enum\Status;
use think\Log;

class PyAttr extends BaseModel
{
    //关系表关联
    public function bindLocation(){
        return $this->hasOne("PyAttr","attr_id","attr_name");
    }
    public function bindYear(){
        return $this->hasOne("PyAttr","attr_id","attr_value");
    }





    //添加地区
    //当出现重复时，返回的提示字符串 $existStr
    public function addData($data,$existStr,$type=Status::location){
        //填充数据
        $data['attr_value']=$data['attr_name'];
        $data['attr_type']=$type;

        //过滤数据
        $data=$this->dataFilter($data);

        //判断重复性
        if($this->checkAddUnique("attr_name",$data['attr_name'])){
            return $existStr;
        }
        return $this->handleReturnData($this->dataAdd($data),"PyAttr addLocation: ");
    }

    //编辑地区
    public function editData($data,$attr_id,$existStr){
        //数据填充
        $data['attr_value']=$data['attr_name'];

        //数据过滤
        $data=$this->dataFilter($data);

        //判断重复性
        if($this->checkEditUnique("attr_name",$data['attr_name'],"attr_id",$attr_id)){
            return $existStr;
        }
        //编辑操作
        return $this->handleReturnData($this->dataEdit($data,"attr_id",$attr_id),"PyAttr editLocation: ");
    }

    //添加区域
    public function addCategory($inputData){
        //组装数据
        $data=[
            "attr_name"=>$inputData['location'],
            "attr_value"=>$inputData['year'],
            "attr_type"=>Status::info
        ];

        //判断数据则重复性
        $condition=[];
        $condition["attr_name"]=['eq',$inputData['location']];
        $condition['attr_value']=['eq',$inputData['year']];
        $condition['attr_type']=['eq',Status::info];
        $isset=$this->where($condition)->find();
        if($isset){
            return "该信息已存在";
        }
        //将数据添加进去
        return $this->handleReturnData($this->dataAdd($data),"PyAttr addCategory: ");
    }


    //分类信息编辑
    public function editCategory($inputData){
        $attr_id=$inputData['attr_id'];
        $data=[
            "attr_name"=>$inputData['location'],
            "attr_value"=>$inputData['year']
        ];

        //校验是否冲突
        //判断数据则重复性
        $condition=[];
        $condition["attr_name"]=['eq',$inputData['location']];
        $condition['attr_value']=['eq',$inputData['year']];
        $condition['attr_type']=['eq',Status::info];
        $condition['attr_id']=['<>',$attr_id];
        $isset=$this->where($condition)->find();
        if($isset){
            return "该信息已存在";
        }

        //将数据编辑
        return $this->handleReturnData($this->dataEdit($data,"attr_id",$attr_id),"PyAttr editCategory: ");
    }
}
